package com.zqweb.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zqweb.common.exception.ExpCodeAndMsgEnum;
import com.zqweb.common.to.SkuStockTo;
import com.zqweb.common.exception.SkuWareException;
import com.zqweb.gulimall.ware.vo.WareLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.ware.entity.WareSkuEntity;
import com.zqweb.gulimall.ware.service.WareSkuService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;



/**
 * 商品库存
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 20:28:38
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 为指定订单锁定库存
     */
    @PostMapping("/lock/order")
    public R lockOrder(@RequestBody WareLockVo wareLockVo){
        try {
            wareSkuService.lockOrder(wareLockVo);
            return R.ok();
        } catch (SkuWareException e) {
            System.out.println(e.getMessage());
        }
        return R.error(ExpCodeAndMsgEnum.SKUWARE_NOSTOCK_EXCEPTION.getCode(),ExpCodeAndMsgEnum.SKUWARE_NOSTOCK_EXCEPTION.getMsg());
    }

    /**
     *  传入Long[] skuIds，查询每个skuId对应的库存容量状态，封装成1个SkuStockTo，最后组装成
     *  1个List<StuStockTo>
     */
    @PostMapping("/hasStock")
    public R hasStock(@RequestBody Long[] skuIds){
        List<SkuStockTo> stockTos = wareSkuService.hasStock(skuIds);
        return R.ok().setData(stockTos);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
